package org.tea.inventory.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tea.inventory.dao.tea.CustomerLevelMapper;
import org.tea.inventory.dao.tea.CustomerSmallMapper;
import org.tea.inventory.domain.sys.UserInfo;
import org.tea.inventory.domain.tea.CustomerSmall;
import org.tea.inventory.domain.tea.CustomerLevel;
import org.tea.inventory.service.ICustomerVipService;
import org.tea.inventory.utils.UserUtil;

import java.util.Date;
import java.util.List;

/**
 * 会员管理信息Service业务层处理
 * 
 * @author dongxb
 * @date 2025-04-27
 */
@Service
public class CustomerViplServiceImpl implements ICustomerVipService
{
    @Autowired
    private CustomerSmallMapper customerSmallMapper;

    @Autowired
    private CustomerLevelMapper customerLevelMapper;

    /**
     * 查询会员管理信息
     * 
     * @param id 会员管理信息主键
     * @return 会员管理信息
     */
    @Override
    public CustomerSmall selectCustomerSmallById(Long id)
    {
        return customerSmallMapper.selectCustomerSmallById(id);
    }

    /**
     * 查询会员管理信息列表
     * 
     * @param customerSmall 会员管理信息
     * @return 会员管理信息
     */
    @Override
    public List<CustomerSmall> selectCustomerSmallList(CustomerSmall customerSmall)
    {
        //只查询未删除的信息
        customerSmall.setDelFlag(0);
        //类型1为会员
        customerSmall.setCustomerType("1");
        List<CustomerSmall> customerSmalls = customerSmallMapper.selectCustomerSmallList(customerSmall);
        customerSmalls.stream().forEach(bean -> {
            bean.setSexStr("0".equals(bean.getSex()) ? "男" : "女");
            bean.setStatusStr(bean.getStatus() == 0 ? "正常" : "已禁用");
        });
        return customerSmalls;
    }

    /**
     * 新增会员管理信息
     * 
     * @param customerSmall 会员管理信息
     * @return 结果
     */
    @Override
    public int insertCustomerSmall(CustomerSmall customerSmall)
    {
        Date date = new Date();
        UserInfo user = UserUtil.getUser();

        int count = customerLevelMapper.selectCustomerLevelByLevel(customerSmall.getCustomerLevel());
        if (count < 1) {
            CustomerLevel customerType = new CustomerLevel();
            customerType.setLevel(customerSmall.getCustomerLevel());
            customerType.setCreateDate(date);
            customerType.setCreateUserId(user.getLoginId());
            customerLevelMapper.insertCustomerLevel(customerType);
        }

        //客户类型为1 会员
        customerSmall.setCustomerType("1");
        customerSmall.setCreateDate(date);
        customerSmall.setCreateUserId(user.getLoginId());
        return customerSmallMapper.insertCustomerSmall(customerSmall);
    }

    /**
     * 修改会员管理信息
     * 
     * @param customerSmall 会员管理信息
     * @return 结果
     */
    @Override
    public int updateCustomerSmall(CustomerSmall customerSmall)
    {
        Date date = new Date();
        UserInfo user = UserUtil.getUser();

        int count = customerLevelMapper.selectCustomerLevelByLevel(customerSmall.getCustomerLevel());
        if (count < 1) {
            CustomerLevel customerType = new CustomerLevel();
            customerType.setLevel(customerSmall.getCustomerLevel());
            customerType.setCreateDate(date);
            customerType.setCreateUserId(user.getLoginId());
            customerLevelMapper.insertCustomerLevel(customerType);
        }

        customerSmall.setUpdateDate(date);
        customerSmall.setUpdateUserId(user.getLoginId());
        return customerSmallMapper.updateCustomerSmall(customerSmall);
    }

    /**
     * 批量删除会员管理信息
     * 
     * @param ids 需要删除的会员管理信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerSmallByIds(Long[] ids)
    {
        for (Long id : ids) {
            CustomerSmall customerSmall = new CustomerSmall();
            customerSmall.setId(id);
            customerSmall.setDelFlag(1);
            customerSmallMapper.updateCustomerSmall(customerSmall);
        }
        return ids.length;
    }

    /**
     * 删除会员管理信息信息
     * 
     * @param id 会员管理信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerSmallById(Long id)
    {
        CustomerSmall customerSmall = new CustomerSmall();
        customerSmall.setId(id);
        customerSmall.setDelFlag(1);
        return customerSmallMapper.updateCustomerSmall(customerSmall);
    }

    /**
     * 更新会员状态
     *
     * @param id 会员信息主键
     * @param status 会员更新前状态
     * @return 结果
     */
    @Override
    public int updateStatus(Long id, Integer status) {
        CustomerSmall customerSmall = new CustomerSmall();
        customerSmall.setId(id);
        customerSmall.setStatus(status == 0 ? 1 : 0);
        customerSmall.setUpdateDate(new Date());
        //customerSmall.setUpdateUserId(UserUtil.getUserInfo().getLoginId());
        return customerSmallMapper.updateCustomerSmall(customerSmall);
    }

    /**
     * 查询会员等级列表
     */
    @Override
    public List<CustomerLevel> selectCustomerLevel(CustomerLevel customerLevel) {
        return customerLevelMapper.selectCustomerLevelList(customerLevel);
    }

    @Override
    public List<CustomerSmall> selectCustomerSmallByCodeOrName(CustomerSmall customerSmall) {
        //类型1为会员
        customerSmall.setCustomerType("1");
        return customerSmallMapper.selectCustomerSmallByCodeOrName(customerSmall);
    }
}
